/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Lenovo
 * Date: 2025-08-16
 * Time: 16:53
 */
//https://leetcode.cn/problems/max-consecutive-ones-iii/description/
public class Test {
    public static int longestOnes(int[] arr, int k) {
        int size=arr.length;
        int max=k;
        int n0=0,n1=0;
        int p1=0,p2=0;
        while(p2<size){
            if(arr[p2]==1){
                n1++;
                max=Math.max(n1,max);
                p2++;
            }
            else{
                if(n0==k){
                    while(arr[p1]==1){
                        n1--;p1++;
                    }
                    n1--;n0--;p1++;
                }
                n0++;n1++;
                max=Math.max(n1,max);
                p2++;
            }
        }
        return max;
    }
    public static void main(String[] args) {
        int[] arr={0,0,1,1,0,0,1,1,1,0,1,1,0,0,0,1,1,1,1};
        System.out.println(longestOnes(arr,3));
    }
}
